package cn.com.callback

import cn.com.callback.ServiceBackInvoke
import cn.com.elite.EliteBean
import groovy.json.*
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import sun.misc.BASE64Encoder

import javax.crypto.BadPaddingException
import javax.crypto.IllegalBlockSizeException
import javax.crypto.NoSuchPaddingException
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import java.security.InvalidAlgorithmParameterException
import java.security.InvalidKeyException
import java.security.MessageDigest
import java.security.NoSuchAlgorithmException

/**
 *
 * @Title: EncryptUtil* AES加密
 * @author: cosson* @date 2020-03-31 21:28:00
 *
 */
public class apiZhswLoginWO implements ServiceBackInvoke {
    private static final Log log = LogFactory.getLog(apiZhswLoginWO.class);

    public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) throws NoSuchAlgorithmException, NoSuchPaddingException, UnsupportedEncodingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
        long startTime = System.currentTimeMillis();
        //response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json;charset=UTF-8");
        request.setCharacterEncoding("utf-8");
        String reJson = null;
        long endTime = 0;
        //获取参数aes_str,需加密的字符串
        def type = request.getParameter("aes_str");

        // 密匙
        def KEY = request.getParameter("aeskey");
        // 偏移量
        def OFFSET = request.getParameter("aesiv");
        // 编码
        def sUser = request.getParameter("sUser");
        //获取账号
        def sPass = request.getParameter("sPass");
        log.debug(sUser + "111111" + sPass);
        //获取密码
        def ENCODING = "UTF-8";
        //算法
        def ALGORITHM = "AES";
        // 默认的加密算法
        def CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding";
        String tokenId = "";
        try {
            if (request.getParameter("sUser") == null || request.getParameter("sPass") == null) {
                endTime = System.currentTimeMillis();
                reJson = "{\"resultcode\":\"1\",\"resultmessage\":\"处理失败(参数输入异常),调用问题处理时长(毫秒):" + (endTime - startTime) + "\",\"data\":[]}";
                return reJson;
            }
            if (sUser == "ZHSW" && sPass == "ZHSW123") {
                log.debug("sUser=" + sUser + "sPass:" + sPass)
                tokenId = makeToken();
                log.debug("token：" + tokenId);
                def isLogin = eliteBean.update("update SYSC_PARAMETER set PARAMETERVALUE = '" + tokenId + "',MODIFIEDDATE=GETDATE() where PARAMETER_ID = 'SWGTOK'", "dataSource");
                log.debug("判断工单是否成功:" + isLogin.toString());
                endTime = System.currentTimeMillis();
                reJson = "{\"resultcode\":\"0\",\"resultmessage\":\"处理完成、调用问题处理时长(毫秒):" + (endTime - startTime) + "\",\"token\":\"" + tokenId + "\",\"code\":\"0\"}";
            } else {
                endTime = System.currentTimeMillis();
                reJson = "{\"resultcode\":\"2\",\"resultmessage\":\"处理失败(用户名或密码不正确!),调用问题处理时长(毫秒):" + (endTime - startTime) + "\",\"data\":[]}";
                return reJson;
            }
        } catch (Exception e) {
            log.error("接口处理异常" + e.getMessage());
            // 失败返回
            reJson = "{\"resultcode\":\"4\",\"resultmessage\":\"接口内部异常:(" + e.getMessage() + ")\",\"data\":[]}";

        }
        return reJson;


    }

    //生成token令牌
    public static String makeToken() {
        String token = (System.currentTimeMillis() + new Random().nextInt(999999999)) + "";
        try {
            MessageDigest md = MessageDigest.getInstance("md5");
            byte[] md5 = md.digest(token.getBytes());
            BASE64Encoder encoder = new BASE64Encoder();
            return encoder.encode(md5);
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
}